<template>
  <div class="categories">
    <!-- 打卡 -->
    <div class="header">
      <router-link to="/Backstage" class="shou">首页</router-link>/ 商品管理 / 分类参数
    </div>
    <div class="tian">
      <el-button @click="tian" type="success" plain>添加分类</el-button>
    </div>
    <!-- 分配角色 -->
    <div class="fen">
      <el-dialog title="添加分类" :visible.sync="dialogFormVisiblec">
        <el-form :model="form" ref="form" class="demo-ruleForm">
          <el-form-item label="分类名称" :label-width="formLabelWidth">
            <el-input v-model.number="form.name" type="password" auto-complete="off"></el-input>
          </el-form-item>
          <el-form-item label="商品分类" :label-width="formLabelWidth">
            <el-cascader
              :options="options"
              :props="{ value:'cat_id',expandTrigger: 'hover',label: 'cat_name' }"
            ></el-cascader>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="fpqx">取 消</el-button>
          <el-button type="primary" @click="fpqd">确 定</el-button>
        </div>
      </el-dialog>
    </div>
    <!-- 表格 -->
    <div>
      <el-table
        :data="tableData"
        height="410px"
        overflow:hidden
        style="width: 100%;margin-bottom: 20px;"
        row-key="cat_id"
        :tree-props="{children: 'children'}"
      >
        <el-table-column prop="cat_name" label="分类名称" width="520"></el-table-column>
        <el-table-column prop="cat_level" label="级别" width="180"></el-table-column>
        <el-table-column prop="cat_deleted" label="是否有效"></el-table-column>
        <el-table-column prop="mg_state" label="操作">
          <template slot-scope="scope">
            <el-button
              type="primary"
              icon="el-icon-edit"
              circle
              @click="zufang(scope.row.cat_name,scope.row.cat_id)"
            ></el-button>
            <el-button type="danger" icon="el-icon-delete" circle @click="open(scope.row.cat_id)"></el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 分页器 -->
    <div class="block">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage4"
        :page-sizes="tiaoshu"
        :page-size="pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="zong"
      ></el-pagination>
    </div>
    <!-- 编辑弹框 -->
    <el-dialog title="修改商品分类" :visible.sync="zuformtwo">
      <el-form :model="formtwo" ref="formtwo" class="demo-ruleForm" label-width="100px">
        <el-form-item label="分类名称">
          <el-input v-model="formtwo.username"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="zuformtwo = false">取 消</el-button>
        <el-button type="primary" :plain="true" @click="zuformtwofang('formtwo')">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { spsjlb,dgoodlist,bgoodlist } from "../../api/api.js";
import axios from "axios";
export default {
  name: "categories",
  data() {
    return {
      form: {
        name: "",
        fen: "",
        value: [],
        options: []
      },
      dialogFormVisiblec: false,
      formLabelWidth: "120px",
      options: [],
      tiaoshu: [5, 10, 15, 20, 25],
      pagesize: 5,
      currentPage4: 1,
      zong: 1,
      tableData: [],
      zuformtwo: false,
      formtwo: {
        username: ""
      },
      id: "",
    };
  },
  methods: {
    fpqx() {},
    fpqd() {},
    //分页器
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.pagesize = val;
      console.log(this.pagesize);
      this.xin();
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.currentPage4 = val;
      console.log(this.currentPage4);
      this.xin();
    },
    //编辑角色传参
    zufang(nam, i) {
      this.zuformtwo = true;
      this.formtwo.username = nam;
      this.id = i;
      console.log(nam);
    },
    //编辑确定接口
    zuformtwofang(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          this.zuformtwo = false;
          bgoodlist({
            id: this.id, //	分类 ID	不能为空携带在url中
            cat_name: this.formtwo.username //	分类名称
          }).then(res => {
            console.log("编辑角色", res);
            if (res.data.meta.status == 200) {
              this.xin();
            }
          });
        } else {
          this.$message({
            showClose: true,
            message: "信息未填",
            type: "warning"
          });
          return false;
        }
      });
    },
    //删除角色
    open(ida) {
      console.log(ida);
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
        center: true
      })
        .then(() => {
          dgoodlist({
            id: ida
          }).then(res => {
            console.log("删除", res);
            if (res.data.meta.status == 200) {
              this.$message({
                type: "success",
                message: "删除成功!"
              });
              this.xin();
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    //数据列表
    xin() {
      spsjlb({
        type: [1, 2, 3],
        pagenum: this.currentPage4,
        pagesize: this.pagesize
      }).then(res => {
        if (res.data.meta.status >= 200 && res.data.meta.status < 300) {
          (this.tableData = res.data.data), (this.zong = res.data.data.length);
          this.tableData = res.data.data.result;
          (this.zong = res.data.data.total),
            (this.currentPage4 = res.data.data.pagenum),
            (this.pagesize = res.data.data.pagesize),
            console.log(22222222222, res);
          for (let i = 0; i < this.tableData.length; i++) {
            if (this.tableData[i].cat_level == 0) {
              this.tableData[i].cat_level = "一级";
            }
            if (this.tableData[i].cat_deleted == false) {
              this.tableData[i].cat_deleted = "有效";
            } else {
              this.tableData[i].cat_deleted = "无效";
            }
            for (let a = 0; a < this.tableData[i].children.length; a++) {
              if (this.tableData[i].children[a].cat_level == 1) {
                this.tableData[i].children[a].cat_level = "二级";
                for (
                  let b = 0;
                  b < this.tableData[i].children[a].children.length;
                  b++
                ) {
                  if (
                    this.tableData[i].children[a].children[b].cat_level == 2
                  ) {
                    this.tableData[i].children[a].children[b].cat_level =
                      "三级";
                  }
                  if (
                    this.tableData[i].children[a].children[b].cat_deleted ==
                    false
                  ) {
                    this.tableData[i].children[a].children[b].cat_deleted =
                      "有效";
                  } else {
                    this.tableData[i].children[a].children[b].cat_deleted =
                      "无效";
                  }
                }
              } else if (this.tableData[i].children[a].cat_level == 2) {
                this.tableData[i].children[a].cat_level = "三级";
              }
              if (this.tableData[i].children[a].cat_deleted == false) {
                this.tableData[i].children[a].cat_deleted = "有效";
              } else {
                this.tableData[i].children[a].cat_deleted = "无效";
              }
            }
          }
        }
        console.log(99999999999, this.tableData);
      });
    },
    //添加分类
    tian() {
      this.dialogFormVisiblec = true;
      spsjlb({
        type: [1, 2, 3]
      }).then(res => {
        console.log(res);
        if (res.data.meta.status >= 200 && res.data.meta.status < 300) {
          this.options = res.data.data;
        }
        console.log(this.options);
      });
    }
  },
  created() {
    this.xin();
    
  }
};
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
.categories {
  height: 532px;
  width: 1620px;
  background-color: #fff;
  border: 1px solid #e9eef3;
  box-shadow: 0 0 10px #ccc;
  border-radius: 5px;
  font-size: 14px;
  padding: 20px;
}
.header {
  color: #606266;
  letter-spacing: 2px;
}
.shou {
  font-weight: bold;
  color: black;
}
.tian {
  margin-top: 10px;
}
</style>
